package com.saascloud.restTemplate.core;

import com.saascloud.core.pojo.R;
import com.saascloud.core.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @Author: liangxu
 * @Contact: one_dream@aliyun.com
 * @Date: 2022/5/30 16:01
 * @Version: 1.0
 * @Description:
 */
public class SaasRestTemplate {

    @Autowired
    private RestTemplate restTemplate;

    public <T> T get(String uri, Map<String, String> uriParam, ParameterizedTypeReference<T> respType) {
        R<User> t1 = null;
        ResponseEntity<T> exchange =
                restTemplate.exchange(uri, HttpMethod.GET, null, respType, uriParam);
        return exchange.getBody();
    }


    public <T> T post(String uri, Object body, ParameterizedTypeReference<T> respType) {
        HttpEntity httpEntity = new HttpEntity(body);
        ResponseEntity<T> exchange =
                restTemplate.exchange(uri, HttpMethod.POST, httpEntity, respType);
        return exchange.getBody();
    }

    public <T> T post(String uri, Object body, ParameterizedTypeReference<T> respType, Map<String, String> uriParam) {
        HttpEntity httpEntity = new HttpEntity(body);
        ResponseEntity<T> exchange =
                restTemplate.exchange(uri, HttpMethod.POST, httpEntity, respType, uriParam);
        return exchange.getBody();
    }


}
